require(['./config'], () => {
    require(['jquery','cookie','gVerify'], () => {
        class Login {
            constructor() {
                this.verifyCode = new GVerify("v_container");
                this.init()
                this.loginR()
                this.yanzen()
            }
            init() {
                const _this=this
                console.log($("#check").prop("checked"));
                $('#login').on('click', function () {
                    const res = _this.verifyCode.validate($("#code_input").val());
                    const name = $('#user-login').val()
                    const pwd = $('#pass-login').val()
                    let userList = localStorage.getItem('userList')
                    if (userList) {
                        userList = JSON.parse(userList)
                        const isExist = userList.some(user => {
                            return user.name === name && user.pwd === pwd
                        })
                        if (isExist&&res) {
                            // 存cookie（存根目录）
                            if($("#check").prop("checked")){
                                $.cookie("name",name, {
                                    expires: 7,
                                    path: '/'
                                })
                            }
                            alert('登录成功，即将跳转首页')
                            window.open('/index.html', '_self')
                        } else {
                            alert('用户名或密码错误或者验证码错误，请重试')
                        }
                    } else {
                        // 数据里没有注册过用户
                        alert('一个用户都没有，请先注册')
                        // window.location.replace('/html/reg.html')
                        var a = $("<a href='/html/reg.html' target='_blank'>Apple</a>").get(0);
                            var e = document.createEvent('MouseEvents');
                            e.initEvent( 'click', true, true );
                            a.dispatchEvent(e); 
                    }
                })
            }
            loginR(){
                $("#login-login").on('click',()=>{
                    location.href = '/html/reg.html'
                })
                $(".back").on('click',()=>{
                    location.href = '/index.html'
                })
            }
            yanzen(){
                const _this=this
                $("#my_button").on("click" ,function(){
                    var res = _this.verifyCode.validate($("#code_input").val());
                    if(res){
                        alert("验证正确");
                    }else{
                        alert("验证码错误");
                    }
                })
            }
            
        }
        new Login()
    })
})